<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\OrderPreset;
use App\Models\OrderPreset as OrderPresetModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class OrderPresetController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new OrderPreset(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('user_id')->view('admin.fields.user_info');
            $grid->column('title');
            $grid->column('order')
                ->display('查看')
                ->modal(function ($modal) {
                    $modal->title('模板');
                    $card = new Card(null, json_encode($this->order));
                    return "<div style='padding:10px 10px 0'>$card</div>";
                });
            $grid->column('sort')->sortable();
            $grid->column('is_default')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user_id');
                $filter->like('title');
                $filter->equal('is_default')->select(OrderPresetModel::$EnumIsDefault);
            });

            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OrderPreset(), function (Show $show) {
            $show->field('id');
            $show->field('order');
            $show->field('user_id');
            $show->field('title');
            $show->field('sort');
            $show->field('is_default');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OrderPreset(), function (Form $form) {
            $form->display('id');
            $form->text('order');
            $form->text('user_id');
            $form->text('title');
            $form->text('sort');
            $form->text('is_default');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
